import axios from 'axios'
import qs from 'qs'
import { Message } from 'element3'
import store from "../store"

let loadingInstance

/** axios封装
 * 请求拦截、相应拦截、错误统一处理
 */
// 环境的切换
//携带证书
axios.defaults.baseURL = '/api'
axios.defaults.timeout = 50000
// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.responseType = 'application/json'
let tokenName = "authorization_code"
//添加一个请求拦截器
axios.interceptors.request.use(function (config) {
    if (store.getters['user/accessToken']){
        config.headers[tokenName] = store.getters['user/accessToken']
    }
    // if (config.data && config.headers['Content-Type'] === 'application/x-www-form-urlencoded;charset=UTF-8') {
    //     config.data = qs.stringify(config.data)
    // }
    // if (debounce.some((item) => config.url.includes(item))) {
    //     //这里写加载动画
    // }
    return config
}, function (error) {
    console.info(error);
    return Promise.reject(error);
});


// 添加一个返回拦截器
axios.interceptors.response.use(
    (response) => {
        console.log(response)
        if (loadingInstance) loadingInstance.close()
        const res = response.data;
        console.log(res)
        if (res.code === 0) {
            return Promise.resolve(response);
        } else {
            if (res.code === 401) {
                Message.error({
                    message: "账户会话过期！请重新登录~",
                    duration: 3 * 1000
                });
                store.dispatch("user/resetToken").then(() => {
                    location.reload();
                })
            } else {
                Message.error({
                    message: res.message || "服务器异常，请稍后重试！",
                    duration: 3 * 1000
                });
            }
            return Promise.reject(response);
        }
    },
    (error) => {
        console.log("error: " + error)
        Message({
            message: error.message,
            type: "error",
            duration: 3 * 1000
        })
        return Promise.reject(error);
    });
//get请求
export function requestGet(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: {
                ...params
            }
        }).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
}

// post请求
export function requestPost(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, qs.stringify(data)).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
}

// delete请求
export function requestDelete(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.delete(url, {
            params: {
                ...params
            }
        }).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
}

// put请求
export function requestPut(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, qs.stringify(params)).then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
}
